package client.controlpanel;

import java.util.ArrayList;

import provider.DataProvider;
import source.DataSource;
import source.RS232Source;
import source.Simulator;

/**
 * Singleton providing configuration values for the application.
 * @author Michal Cmil
 */
public class Configuration {
	protected static Configuration instance;
	
	private DataSource activeDataSource; 
	private DataProvider dataProvider = new DataProvider();
	
	private ArrayList<String> availableDataSources;
	private String portName = "COM1";
	
	protected Configuration() {
		setActiveDataSource(new Simulator()); //Simulator is the default DS
		availableDataSources = new ArrayList<String>();
		availableDataSources.add(new RS232Source().getConfigId());
		availableDataSources.add(new Simulator().getConfigId());
	}
	
	public static Configuration getInstance() {
		if (instance == null) {
			instance = new Configuration();
		}
		return instance;
	}

	public void setActiveDataSource(DataSource activeDataSource) {
		if(this.activeDataSource != null)
			this.activeDataSource.stopGatheringData();

		this.activeDataSource = activeDataSource;
		this.activeDataSource.addListner(dataProvider);
	}
	
	public void setActiveDataSource(String configId) {
		if(configId == new RS232Source().getConfigId()) { 
			setActiveDataSource(new RS232Source());
		}
		else 
			setActiveDataSource(new Simulator());
	}

	public DataSource getActiveDataSource() {
		return activeDataSource;
	}
	
	public DataProvider getDataProvider() {
		return dataProvider;
	}
	
	public ArrayList<String> getAvailableDataSources() {
		return availableDataSources;
	}

	public void setPortName(String portName) {
		this.portName = portName;
	}

	public String getPortName() {
		return portName;
	}

	public ArrayList<String> getAvailablePorts() {
		return (ArrayList<String>) new RS232Source().getSerialPorts();
	}
	
}
